package com.zhongwang.business.module.basic.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.module.basic.model.BusinessGroupModel;
import com.zhongwang.business.module.basic.service.BusinessGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BusinessGroupController extends BaseController {
    @Autowired
    private BusinessGroupService businessGroupService;
    /**
     * 商务组列表
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/businessGroup/groupList")
    public void selectBusinessGroupList(Model model) {

        /*这部分显示页面上的checkBox列表的值*/
        BusinessGroupModel cbModel=new BusinessGroupModel();
        cbModel.setGtype(0);
        model.addAttribute("chengbenlist", businessGroupService.selectBusinessGroupList(cbModel));

        BusinessGroupModel htModel=new BusinessGroupModel();
        htModel.setGtype(1);
        model.addAttribute("hetonglist", businessGroupService.selectBusinessGroupList(htModel));
    }

/**
 * 商务组列表
 * @param
 * @return
 */
    @ResponseBody
    @PostMapping("/businessGroup/groupAllList")
    @RequiresPermissions({"basic:working:view"})
    public JsonResult selectAllBusinessGroupList() {
        JsonResult jsonResult=new JsonResult();
        return jsonResult(businessGroupService.selectAllGroupList());
    }

    /**
     * 待下达列表页面
     * @return
     */
    @GetMapping("/groupAllList")
    public String linkTaskBoardRelease(Model model) {
       List<BusinessGroupModel> depts = businessGroupService.selectDept();
        model.addAttribute("depts",depts);
        return "/taskManage/basic/groupInfo";
    }

    /**
     * 执行添加
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/businessGroup/insert")
    public JsonResult insertBusinessGroupList(BusinessGroupModel model) {
        JsonResult jsonResult = new JsonResult();
        model.setDeptId(Integer.parseInt(model.getDeptName()));
        Integer flg=businessGroupService.addGroupInfo(model);
        if(flg==1){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    /**
     * 执行删除
     * @return
     */
    @ResponseBody
    @PostMapping("/businessGroup/del")
    public JsonResult delBusinessGroupList(Integer id) {
        JsonResult jsonResult = new JsonResult();
        Integer flg=businessGroupService.delGroupInfo(id);
        if(flg==1){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }
    @ResponseBody
    @PostMapping("/businessGroup/selOneData")
    public BusinessGroupModel selectAllGroupById(Integer id){
        return businessGroupService.selectAllGroupById(id);
    }



    /**
     * 执行删除
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/businessGroup/upd")
    public JsonResult delBusinessGroupList(BusinessGroupModel model) {
        JsonResult jsonResult = new JsonResult();
        Integer flg=businessGroupService.updGroupInfo(model);
        if(flg==1){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("编辑成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("编辑失败");
        }
        return jsonResult;
    }
}
